package com.tentative.competition.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @auther: Smile Huang
 * @date: 2023/4/27 11:38
 * @desc:
 * @version: 2.0
 */
@Component
public class NumberUtils {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static String WORKS_CODE_PREFIX = "W02";

    private static String WORKS_CODE_PREFIX_01 = "W01";
    private static String PLAN_CODE_PREFIX = "P02";
    private static String KEY_PREFIX = "ACCOUNT:";


    /**
     *  XJ + 5位自增码
     * 编码规则：XJ00001
     * @return
     */
    public String getPlanRuleCode(){
        //redis的key
        String applyIdKey = KEY_PREFIX + PLAN_CODE_PREFIX;
        if (stringRedisTemplate.hasKey(applyIdKey)) {
            stringRedisTemplate.opsForValue().increment(applyIdKey,1);
        } else {
            stringRedisTemplate.opsForValue().set(applyIdKey, "1");
        }
        String codeNum = stringRedisTemplate.opsForValue().get(applyIdKey);
        return PLAN_CODE_PREFIX + String.format("%04d", Integer.valueOf(codeNum));
    }

    /**
     * 获取作品编码
     * @return
     */
    public String getWorksRuleCode(String num){
        //redis的key
        String applyIdKey = KEY_PREFIX + String.format("%01d", Integer.valueOf(num));
        if (stringRedisTemplate.hasKey(applyIdKey)) {
            stringRedisTemplate.opsForValue().increment(applyIdKey,1);
        } else {
            stringRedisTemplate.opsForValue().set(applyIdKey, "1");
        }
        String codeNum = stringRedisTemplate.opsForValue().get(applyIdKey);
        return WORKS_CODE_PREFIX + String.format("%04d", Integer.valueOf(codeNum));
    }

    public String getWorksRuleCode01(){
        //redis的key
        String applyIdKey = KEY_PREFIX + WORKS_CODE_PREFIX_01;
        if (stringRedisTemplate.hasKey(applyIdKey)) {
            stringRedisTemplate.opsForValue().increment(applyIdKey,1);
        } else {
            stringRedisTemplate.opsForValue().set(applyIdKey, "1");
        }
        String codeNum = stringRedisTemplate.opsForValue().get(applyIdKey);
        return WORKS_CODE_PREFIX_01 + String.format("%04d", Integer.valueOf(codeNum));
    }

}
